package com.miwzn.boot.modules.sys.web;

import com.google.gson.Gson;
import com.miwzn.base.AbstractController;
import com.miwzn.boot.modules.oss.cloud.AliyunCloudStorageService;
import com.miwzn.boot.modules.oss.cloud.CloudStorageConfig;
import com.miwzn.boot.modules.oss.cloud.QcloudCloudStorageService;
import com.miwzn.boot.modules.oss.cloud.QiniuCloudStorageService;
import com.miwzn.boot.modules.sys.entity.po.SysConfig;
import com.miwzn.boot.modules.sys.entity.po.SysOss;
import com.miwzn.boot.modules.sys.entity.qm.SysOssQuery;
import com.miwzn.boot.modules.sys.entity.dto.SysOssDto;
import com.miwzn.boot.modules.sys.service.SysConfigService;
import com.miwzn.boot.modules.sys.service.SysOssService;
import com.miwzn.comm.ResponseEntity;
import com.miwzn.comm.annotation.OperationLog;
import com.miwzn.comm.constant.ConfigConstant;
import com.miwzn.comm.constant.MyConstant;
import com.miwzn.comm.utils.validate.AliyunGroup;
import com.miwzn.comm.utils.validate.QcloudGroup;
import com.miwzn.comm.utils.validate.QiniuGroup;
import com.miwzn.comm.utils.validate.ValidatorUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: wzn
 * @Date: 2019-02-20 17:12:52
 * @Description: www.miwzn.com mvc api
 */
@ResponseBody
@Controller
@RequestMapping("sys/oss")
public class SysOssController extends AbstractController<SysOss, SysOssDto, SysOssQuery> {
    private static Logger logger = LoggerFactory.getLogger(SysOssController.class);

    @Autowired
    private SysOssService sysOssService;

    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 根据ID获取信息
     *
     * @param id
     *
     * @return
     */
    @RequiresPermissions("sys:oss:getById")
    @GetMapping("/getById/{id}")
    public ResponseEntity getById(@PathVariable("id") String id) {
        return super.getById(id);
    }

    /**
     * 列表
     *
     * @param entity
     *
     * @return
     */
    @RequiresPermissions("sys:oss:list")
    @GetMapping("/list")
    public ResponseEntity list(SysOssQuery entity) {
        return super.listD(entity);
    }

    /**
     * 添加
     *
     * @param config
     *
     * @return
     */
    @OperationLog(operation = "系统-OSS", content = "保存OSS云存储配置")
    @RequiresPermissions("sys:oss:config:save")
    @PostMapping("config/save")
    public ResponseEntity save(@RequestBody CloudStorageConfig config) {

        //校验类型
        ValidatorUtils.validateEntity(config);
        if (config.getType().equals(ConfigConstant.OSSCloud.QINIUCLOUD.getValue())) {
            //校验七牛数据
            ValidatorUtils.validateEntity(config, QiniuGroup.class);
        } else if (config.getType().equals(ConfigConstant.OSSCloud.ALIYUN.getValue())) {
            //校验阿里云数据
            ValidatorUtils.validateEntity(config, AliyunGroup.class);
        } else if (config.getType().equals(ConfigConstant.OSSCloud.QCLOUD.getValue())) {
            //校验腾讯云数据
            ValidatorUtils.validateEntity(config, QcloudGroup.class);
        }

        SysConfig query = new SysConfig();
        query.setConfigKey(ConfigConstant.CLOUD_STORAGE_CONFIG_KEY);
        query.setDelFlag(MyConstant.FLAG_N);
        List<SysConfig> select = sysConfigService.select(query);
        if (CollectionUtils.isNotEmpty(select)) {
            for (SysConfig sysConfig : select) {
                sysConfig.setStatus(config.getStatus());
                sysConfig.setConfigValue(new Gson().toJson(config));
                sysConfigService.updateByPrimaryKey(sysConfig);
            }
        } else {
            SysConfig entity = new SysConfig();
            entity.setConfigKey(ConfigConstant.CLOUD_STORAGE_CONFIG_KEY);
            entity.setStatus(config.getStatus());
            entity.setConfigValue(new Gson().toJson(config));
            sysConfigService.insert(entity);
        }
        return ResponseEntity.success("保存成功");
    }

    /**
     * 添加
     *
     *
     * @return
     */
    @OperationLog(operation = "系统-OSS", content = "获取OSS云存储配置")
    @RequiresPermissions("sys:oss:getConfig")
    @GetMapping("getConfig")
    public ResponseEntity getOssConfig() {
        final CloudStorageConfig configObject = sysConfigService.getConfigObject(ConfigConstant.CLOUD_STORAGE_CONFIG_KEY, CloudStorageConfig.class);
        return ResponseEntity.success("保存成功").put("data",configObject);
    }

    /**
     * 软删除
     *
     * @param id
     *
     * @return
     */
    @RequiresPermissions("sys:oss:delFlag")
    @DeleteMapping("/delFlag/{id}")
    public ResponseEntity deleteFlag(@PathVariable("id") String id) {
        return super.deleteFlag(id);
    }

    /**
     * 软删除
     *
     * @param ids
     *
     * @return
     */
    @RequiresPermissions("sys:oss:delFlag")
    @DeleteMapping("/delFlag")
    public ResponseEntity deleteFlag(@RequestBody String[] ids) {
        return super.deleteFlag(ids);
    }

    /**
     * 硬删除
     *
     * @param id
     *
     * @return
     */
    @RequiresPermissions("sys:oss:del")
    @DeleteMapping("/del/{id}")
    public ResponseEntity delete(@PathVariable("id") String id) {
        return super.delete(id);
    }

    /**
     * 硬删除
     *
     * @param ids
     *
     * @return
     */
    @RequiresPermissions("sys:oss:del")
    @DeleteMapping("/del")
    public ResponseEntity delete(@RequestBody String[] ids) {
        return super.delete(ids);
    }

}
